const debug = true;
const log = debug
  ? console.log.bind("***debug ***", console)
  : function () {};
 
const e = (selector) => document.querySelector(selector);

const now = () => {
  let d = new Date();
  // 年
  let nm = d.getFullYear();
  // 月
  let yt = d.getMonth() + 1;
  // 号
  let ri = d.getDate();
  let ui = d.getHours();
  let ff = d.getMinutes();
  let mc = d.getSeconds();
  return [nm, yt, ri, ui, ff, mc].map((n) => (n < 10 ? `0${n}` : n));
};

// 更新时间和日期
const updateCountdown = () => {
  const [y, mon, d, h, min, s] = now();
  const date = `${y}.${mon}.${d}`;
  e("#year").innerHTML = date;
  e("#hours").innerHTML = h;
  e("#seconds").innerHTML = s;
  e("#minutes").innerHTML = min;
};

const render = () => {
  updateCountdown();

  e(".loading").classList.add("hide");
  e(".countdown").classList.remove("hide");
};


/**
 * 1. 先加载 loading 
 * 2. 显示当前时间
 */
const __main = () => {
  // 过一秒钟，显示时间
  setTimeout(() => {
    render();
  }, 1000);
  // 不断更新当前时间
  setInterval(updateCountdown, 1000);
};

__main();
